﻿// DigitalDesign
// Project: InformationBoard
// Developer: Baevskih Aleksandr
// Created: 14.11.2011
// Last change: 16.11.2011

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Server
{
    [DataContract]
    public class PlaylistWarp
    {
        DataModelContainer dataBase = new DataModelContainer();
        int _clientID = 0;

        public PlaylistWarp(int ClientID)
        {
            _clientID = ClientID;
            Version = new wClientChanx();
        }

        public int CreateWarp(string DayOfWeek, bool ToAdmin)
        {
            var _dayID = 0;
            Collection = new List<PlaylistElement>();

            try 
            { 
                _dayID = (from _days in dataBase.DaysOfWeeks
                          where DayOfWeek == _days.Name
                          select _days).First().Id; 
            }
            catch { return -1; }

            this.DayOfWeek = DayOfWeek;

            try
            {
                foreach (Playlist tempPlaylist in (from _playlist in dataBase.Playlists
                                                   where _playlist.DaysOfWeek_Id == _dayID && _playlist.ClientDevice_Id == _clientID
                                                   select _playlist))
                {
                    Collection.Add(new PlaylistElement((from _videos in dataBase.Videos 
                                                        where _videos.Id == tempPlaylist.Video_Id
                                                        select _videos).First().FileName,
                                                        tempPlaylist.TimeOfBegin, tempPlaylist.TimeOfEnd));

                    //Если надо отсылать администратору, то записываем в обертку ИД плейлиста
                    Collection.Last().ID = ToAdmin ? tempPlaylist.Id : -1;
                }
            }
            catch { return 1; }

            if (Collection.Count == 0) return 1;

            return 0;
        }

        [DataMember]
        public List<PlaylistElement> Collection
        {
            get;
            set;
        }

        [DataMember]
        public string DayOfWeek
        {
            get;
            set;
        }

        [DataMember]
        public wClientChanx Version
        {
            get;
            set;
        }
    }
}
